package com.yalu.action.data;

import java.io.FileOutputStream;
import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.yalu.action.base.BaseAction;
import com.yalu.service.data.BrandReportService;
import com.yalu.service.data.ItemsalerankReportService;
import com.yalu.service.data.NetworkioReportService;
import com.yalu.service.data.ProductyearsReportService;
import com.yalu.service.data.SalesReportService;
import com.yalu.util.ExcelDownload;

import core.rpt.DailySalesReport;

@ParentPackage("data")
public class ExcelExportAction extends BaseAction {

	private static final long serialVersionUID = 7387681946571752301L;
	private static String FILE_NAME = "DailySales.xls";
	private static String RESULT_FILE_NAME = "Result.xls";
	private String beginTime;
	private String endTime;
	private String compareYear;
	private String dateType;
	
	@Resource
	private SalesReportService salesReportService;
	@Resource
	private NetworkioReportService networkioReportService;
	@Resource
	private BrandReportService brandReportService;
	@Resource
	private ProductyearsReportService productyearsReportService;
	@Resource
	private ItemsalerankReportService itemsalerankReportService;

	
	public String export() {
		pageFlag = "export";
		String templateFilePath = getApplication()
				.getRealPath("/excelTemplate")
				+ "/" + FILE_NAME;
		Workbook workbook;
		try {
			workbook = DailySalesReport.genReport(
					beginTime, endTime, null);
		String resultFileName = getApplication().getRealPath("/excelTemplate")
				+ "/" + RESULT_FILE_NAME;
		workbook.write(new FileOutputStream(resultFileName));

		HttpServletResponse response = ServletActionContext.getResponse();
		ExcelDownload downLoad = new ExcelDownload();
		downLoad.downLoad(resultFileName, FILE_NAME, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String exportSalesReport() {
		beginTime = beginTime.replace("/", "");
		endTime = endTime.replace("/", "");
		String resultFileName = getApplication().getRealPath("/exportReportExcel")
				+ "/" + RESULT_FILE_NAME;
		salesReportService.createReport(dateType,beginTime,endTime,resultFileName,compareYear);
		HttpServletResponse response = ServletActionContext.getResponse();
		ExcelDownload downLoad = new ExcelDownload();
		try {
			downLoad.downLoad(resultFileName, FILE_NAME, response);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String exportNetworkioReport() {
		beginTime = beginTime.replace("/", "");
		endTime = endTime.replace("/", "");
		String resultFileName = getApplication().getRealPath("/exportReportExcel")
				+ "/" + RESULT_FILE_NAME;
		networkioReportService.createReport(dateType,beginTime,endTime,resultFileName);
		HttpServletResponse response = ServletActionContext.getResponse();
		ExcelDownload downLoad = new ExcelDownload();
		try {
			downLoad.downLoad(resultFileName, FILE_NAME, response);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String exportBrandReport() {
		beginTime = beginTime.replace("/", "");
		endTime = endTime.replace("/", "");
		String resultFileName = getApplication().getRealPath("/exportReportExcel")
				+ "/" + RESULT_FILE_NAME;
		brandReportService.createReport(dateType,beginTime,endTime,resultFileName);
		HttpServletResponse response = ServletActionContext.getResponse();
		ExcelDownload downLoad = new ExcelDownload();
		try {
			downLoad.downLoad(resultFileName, FILE_NAME, response);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	public String exportProductyearsReport() {
		beginTime = beginTime.replace("/", "");
		endTime = endTime.replace("/", "");
		String resultFileName = getApplication().getRealPath("/exportReportExcel")
				+ "/" + RESULT_FILE_NAME;
		productyearsReportService.createReport(dateType,beginTime,endTime,resultFileName);
		HttpServletResponse response = ServletActionContext.getResponse();
		ExcelDownload downLoad = new ExcelDownload();
		try {
			downLoad.downLoad(resultFileName, FILE_NAME, response);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	public String exportItemsalerankReport() {
		beginTime = beginTime.replace("/", "");
		endTime = endTime.replace("/", "");
		String resultFileName = getApplication().getRealPath("/exportReportExcel")
				+ "/" + RESULT_FILE_NAME;
		itemsalerankReportService.createReport(dateType,beginTime,endTime,resultFileName);
		HttpServletResponse response = ServletActionContext.getResponse();
		ExcelDownload downLoad = new ExcelDownload();
		try {
			downLoad.downLoad(resultFileName, FILE_NAME, response);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	

	public String getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(String beginTime) {
		this.beginTime = beginTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getDateType() {
		return dateType;
	}

	public void setDateType(String dateType) {
		this.dateType = dateType;
	}

	public String getCompareYear() {
		return compareYear;
	}

	public void setCompareYear(String compareYear) {
		this.compareYear = compareYear;
	}

}
